# #!/usr/bin/env python
# # -*- encoding: utf-8 -*-
# '''
# @File    :   App_WriteDataById_0x2E_test.py
# @Time    :   2022/8/25 13:31:28
# @Author  :   yangliang
# @Version :   1.0
# @Contact :   yangliang@chengtech.com
# @License :   (C)Copyright 2022-2025
# @Desc    :   None
# '''

# # here put the import lib
# from lib2to3.pgen2 import driver
# from msilib.schema import Upgrade
# from operator import length_hint
# from pytest_assume.plugin import assume

# import pytest
# import logging
# import time
# import requests
# from sympy import I
# # from sympy import assuming
# from commDriver.TS_CAN import *
# import allure
# import sys
# from collections.abc import Iterable

# from sqlalchemy import false
# sys.dont_write_bytecode = True

# class Test_Upgrade_Software(object):

#     @allure.severity('critical')
#     @allure.title('Test software upgrade at voltage 14.00v')
#     @allure.description('Test software upgrade at voltage 14.00v')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_Normal_SoftwareUpgradeFlow(self, action, Driver):
#         self.SoftwareUpgradeFlow(action,Driver,14)

#     @allure.severity('critical')
#     @allure.title('Test software upgrade at voltage 9.00v')
#     @allure.description('Test software upgrade at voltage 9.00v')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_Low_Voltage_9V_SoftwareUpgradeFlow(self, action, Driver):
#         self.SoftwareUpgradeFlow(action,Driver,9)

#     @allure.severity('critical')
#     @allure.title('Test software upgrade at voltage 16.00v')
#     @allure.description('Test software upgrade at voltage 16.00v')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_High_Voltage_16V_SoftwareUpgradeFlow(self, action, Driver):
#         self.SoftwareUpgradeFlow(action,Driver,16)

#     @allure.severity('critical')
#     @allure.title('Test repeatedly software upgrade')
#     @allure.description('Test repeatedly software upgrade')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_SoftwareUpgradeFlow_repeatedly(self, action, Driver):
#         self.SoftwareUpgradeFlow(action,Driver,14)
#         time.sleep(2)
#         self.SoftwareUpgradeFlow(action,Driver,14)

#     @allure.severity('critical')
#     @allure.title('Test software upgrade quit at 30% quit')
#     @allure.description('Test software upgrade quit at 30% quit')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_SoftwareUpgradeFlow_Termination30_Quit(self, action, Driver):
#         self.SoftwareUpgradeFlow_Termination(action,Driver,30, 6)
    
#     @allure.severity('critical')
#     @allure.title('Test software upgrade quit at 50%')
#     @allure.description('Test software upgrade quit at 50%')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_SoftwareUpgradeFlow_Termination50_Quit(self, action, Driver):
#         self.SoftwareUpgradeFlow_Termination(action,Driver,50, 6)
    
#     @allure.severity('critical')
#     @allure.title('Test software upgrade at 60% power down')
#     @allure.description('Test software upgrade at 60% power down')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_SoftwareUpgradeFlow_PowerDown(self, action, Driver):
#         self.SoftwareUpgradeFlow_Termination(action,Driver,60, 0)

#     @allure.severity('critical')
#     @allure.title('Test software upgrade Termination at 60% ')
#     @allure.description('Test software upgrade Termination at 60%')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_SoftwareUpgradeFlow_Termination(self, action, Driver):
#         self.SoftwareUpgradeFlow_Termination(action,Driver,60, 1)

#     @allure.severity('critical')
#     @allure.title('Test software upgrade CanH Short VBat at 60% ')
#     @allure.description('Test software upgrade CanH Short VBat at 60%')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_SoftwareUpgradeFlow_CanHShortVBat(self, action, Driver):
#         self.SoftwareUpgradeFlow_Termination(action,Driver,60, 2)

#     @allure.severity('critical')
#     @allure.title('Test software upgrade CanH Short Gnd at 60% ')
#     @allure.description('Test software upgrade CanH Short Gnd at 60%')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_SoftwareUpgradeFlow_CanHShortGND(self, action, Driver):
#         self.SoftwareUpgradeFlow_Termination(action,Driver,60, 3)
#     @allure.severity('critical')
#     @allure.title('Test software upgrade CanH Short CanL at 60% ')
#     @allure.description('Test software upgrade CanH Short CanL at 60%')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_SoftwareUpgradeFlow_CanHShortCanL(self, action, Driver):
#         self.SoftwareUpgradeFlow_Termination(action,Driver,60, 4)

#     @allure.severity('critical')
#     @allure.title('Test software upgrade disconnect CanH at 60% ')
#     @allure.description('Test software upgrade disconnect CanH at 60%')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_SoftwareUpgradeFlow_DisconnectCanH(self, action, Driver):
#         self.SoftwareUpgradeFlow_Termination(action,Driver,60, 5)


#     @allure.severity('critical')
#     @allure.title('Test software upgrade power down Front Erase memory')
#     @allure.description('Test software upgrade Front Erase memory power down')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_FrontEraseMemory_PowerDown(self, action, Driver):
#         self.SoftwareUpgradeFlow_EraseMemory(action,Driver, 0)


#     @allure.severity('critical')
#     @allure.title('Test software upgrade CanH Short VBat Front Erase memory')
#     @allure.description('Test software upgrade CanH Short VBat at 60%')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_FrontEraseMemory_CanHShortVBat(self, action, Driver):
#         self.SoftwareUpgradeFlow_EraseMemory(action,Driver, 2)

#     @allure.severity('critical')
#     @allure.title('Test software upgrade CanH Short Gnd Front Erase memory')
#     @allure.description('Test software upgrade CanH Short Gnd Front Erase memory')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_FrontEraseMemory_CanHShortGND(self, action, Driver):
#         self.SoftwareUpgradeFlow_EraseMemory(action,Driver,3)

#     @allure.severity('critical')
#     @allure.title('Test software upgrade CanH Short CanL Front Erase memory')
#     @allure.description('Test software upgrade CanH Short CanL Front Erase memory')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_FrontEraseMemory_CanHShortCanL(self, action, Driver):
#         self.SoftwareUpgradeFlow_EraseMemory(action,Driver,4) 

#     @allure.severity('critical')
#     @allure.title('Test software upgrade disconnect CanH Front Erase memory')
#     @allure.description('Test software upgrade disconnect Front Erase memory')
#     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
#     def test_FrontEraseMemory_DisconnectCanH(self, action, Driver):
#         self.SoftwareUpgradeFlow_EraseMemory(action,Driver, 5)

#     # 完整下载
#     def SoftwareUpgradeFlow(self, action, Driver, Voltage):
#         iStep = 1
#         with allure.step(f'Step{iStep}: Set voltage to {Voltage} V'): 
#             Driver['power'].set_vltage(Voltage)
#             time.sleep(1)
#             Driver['power'].Read_Measure()
#             iStep = iStep + 1
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'1003','5003',0,iStep)               # 诊断会话
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'31010203','7101020300',0,iStep)     # 更新条件查询
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'1083', '',0,iStep, False, 'UDSFun')                # 进入诊断会话
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'8582', '',0,iStep, False, 'UDSFun')               # 禁止故障码
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'288101', '',0,iStep, False, 'UDSFun')             # 禁止通讯报文发送
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'3E00', '7E00',0,iStep)           
#         iStep = Driver['CAN'].unlock(Driver,iStep)                                       # 安全解锁
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1002', '5002',0,iStep)               # 进入编程会话模式
#         iStep = Driver['CAN'].unlock(Driver,iStep)                                       # 安全解锁
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)               

#         '''Flash Driver 下载'''
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '340044+地址+长度', '7420x1x2',0,iStep)    # 请求下载
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '36', '76',0,iStep)                       # 数据传输
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '37', '77',0,iStep)                       # 传输退出
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010202x1x2x3x4', '7101020200',0,iStep) # 例行控制，检查传输序列
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)               

#         '''应用数据下载'''
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3101FF0044+地址+长度', '7101FF00XX',0,iStep)  # 例行控制，擦除内存
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '340044+地址+长度', '7420x1x2',0,iStep)        # 请求下载
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3601xxxxxx', '7601',0,iStep)                 # 数据传输
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3602xxxxxx', '7602',0,iStep)                 # 数据传输
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '37', '77',0,iStep)                           # 请求退出传输
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)              
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010202x1x2x3x4', '7101020200',0,iStep)     # 例行控制，检查传输序列
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010204x1x2x3x4', '7101020400',0,iStep)     # 例行控制--$0204数字签名 选用步骤
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3101FF01', '7101FF0100',0,iStep)             # 例行控制，软硬件一致性检查
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1101', '5101',0,iStep)                       # ECU复位，硬件复位
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '288001', '',0,iStep, False, 'UDSFun')                     # 恢复通讯报文的发送
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '8581', '',0,iStep)                           # 恢复故障码设置
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)               

#     # 下载中断 schedule=进度 terminationType=中断类型[0=断电 1=终止传输 2=CAN_H短接VBAT 3=CAN_H短接GND  4=CAN_H短接CAN_L 5=断开CANH 6=退出]
#     def SoftwareUpgradeFlow_Termination(self, action, Driver, schedule, terminationType):
#         iStep = 1

#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'1003','5003',0,iStep)               # 诊断会话
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'31010203','7101020300',0,iStep)     # 更新条件查询
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'1083', '',0,iStep, False, 'UDSFun')               # 进入诊断会话
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'8582', '',0,iStep, False, 'UDSFun')               # 禁止故障码
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'288101', '',0,iStep, False, 'UDSFun')              # 禁止通讯报文发送
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'3E00', '7E00',0,iStep)               
#         iStep = Driver['CAN'].unlock(Driver,iStep)                                       # 安全解锁
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1002', '5002',0,iStep)               # 进入编程会话模式
#         iStep = Driver['CAN'].unlock(Driver,iStep)                                       # 安全解锁
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)               

#         '''Flash Driver 下载'''
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '340044+地址+长度', '7420x1x2',0,iStep)    # 请求下载
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '36', '76',0,iStep)                       # 数据传输
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '37', '77',0,iStep)                       # 传输退出
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010202x1x2x3x4', '7101020200',0,iStep) # 例行控制，检查传输序列
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)               

#         '''应用数据下载'''
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3101FF0044+地址+长度', '7101FF00XX',0,iStep)  # 例行控制，擦除内存
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '340044+地址+长度', '7420x1x2',0,iStep)        # 请求下载

#         length = int(800/63) + 1
#         len = int(length * schedule)
#         for i in range(len):
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, f'36{i:02X}xxxxxx', '7601',0,iStep)                 # 数据传输
#         if terminationType == 0:
#             # Power Down
#             Driver['power'].Enable(False)
#             time.sleep(1)
#             # Power On
#             Driver['power'].Enable(True)
#             Driver['power'].set_vltage(12)
#             time.sleep(3)
#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
#         elif terminationType == 1:
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '37', '77',0,iStep)                           # 请求退出传输
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)                       # 
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010202x1x2x3x4', '7101020201',0,iStep)     # 例行控制，检查传输序列
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010204x1x2x3x4', '7101020401',0,iStep)     # 例行控制--$0204数字签名 选用步骤
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3101FF01', '7101FF0101',0,iStep)             # 例行控制，软硬件一致性检查
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1101', '5101',0,iStep)                       # ECU复位，硬件复位
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '288001', '',0,iStep, False, 'UDSFun')                     # 恢复通讯报文的发送
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '8581', '',0,iStep)                           # 恢复故障码设置
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)                       # 
#         elif terminationType == 2:
#             # CAN_H短接VBAT

#             '''后续实现代码内容'''

#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
#         elif terminationType == 3:
#             # CAN_H短接GND

#             '''后续实现代码内容'''

#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
#         elif terminationType == 4:
#             # CAN_H短接CAN_L

#             '''后续实现代码内容'''

#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
#         elif terminationType == 5:
#             # CAN_H断开

#             '''后续实现代码内容'''

#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
#         elif terminationType == 6:
#             iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1001', '5001',0,iStep)                           # 请求退出传输
#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)


#     # 内存擦除前中断 schedule=进度 terminationType=中断类型[0=断电 1=退出 2=CAN_H短接VBAT 3=CAN_H短接GND  4=CAN_H短接CAN_L 5=断开CANH]
#     def SoftwareUpgradeFlow_EraseMemory(self, action, Driver, terminationType):
#         iStep = 1

#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'1003','5003',0,iStep)               # 诊断会话
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'31010203','7101020300',0,iStep)     # 更新条件查询
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'1083', '',0,iStep, False, 'UDSFun')                # 进入诊断会话
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'8582', '',0,iStep, False, 'UDSFun')               # 禁止故障码
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'288101', '',0,iStep, False, 'UDSFun')              # 禁止通讯报文发送
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver,'3E00', '7E00',0,iStep)               
#         iStep = Driver['CAN'].unlock(Driver,iStep)                                       # 安全解锁
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '1002', '5002',0,iStep)               # 进入编程会话模式
#         iStep = Driver['CAN'].unlock(Driver,iStep)                                       # 安全解锁
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)               

#         '''Flash Driver 下载'''
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '340044+地址+长度', '7420x1x2',0,iStep)    # 请求下载
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '36', '76',0,iStep)                       # 数据传输
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '37', '77',0,iStep)                       # 传输退出
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010202x1x2x3x4', '7101020200',0,iStep) # 例行控制，检查传输序列
#         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E00', '7E00',0,iStep)               


#         if terminationType == 0:
#             # Power Down
#             Driver['power'].Enable(False)
#             time.sleep(1)
#             # Power On
#             Driver['power'].Enable(True)
#             Driver['power'].set_vltage(12)
#             time.sleep(3)
#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
#         elif terminationType == 2:
#             # CAN_H短接VBAT

#             '''后续实现代码内容'''

#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
#         elif terminationType == 3:
#             # CAN_H短接GND

#             '''后续实现代码内容'''

#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
#         elif terminationType == 4:
#             # CAN_H短接CAN_L

#             '''后续实现代码内容'''

#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
#         elif terminationType == 5:
#             # CAN_H断开

#             '''后续实现代码内容'''

#             # 再次上电后重新下载
#             self.SoftwareUpgradeFlow(Driver, 14)
        
#         '''应用数据下载'''
#         # iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3101FF0044+地址+长度', '7101FF00XX',0,iStep)  # 例行控制，擦除内存
#         # iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '340044+地址+长度', '7420x1x2',0,iStep)        # 请求下载
        
        
